﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Framework.Common;
using System.Net.Mail;
using Framework.Common.Configs;
using Framework.Common.Sql;
using System.Data;
using System.Threading;

namespace ConsoleEmail
{
    class Program
    {
        static void Main(string[] args)
        {
            var config = CustomConfig<EmailSql>.GetConfig();

            Console.WriteLine(config.ZipingSql);
            LogHelper.Debug(config.ZipingSql);
            if (!config.ZipingSql.IsBlank())
            {
                var ds = SqlHelper.ExecuteDataset(config.ConnectionString, CommandType.Text, config.ZipingSql);
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    var r = ds.Tables[0].Rows[i];
                    string a = r["supname"] + ":" + SendZiping(r["supname"], r["supmail"]);
                    LogHelper.Debug(a);
                    Console.WriteLine(a);
                    Thread.Sleep(5000);
                }
            }

            Console.WriteLine(config.XiashuSql);
            LogHelper.Debug(config.XiashuSql);

            if (!config.XiashuSql.IsBlank())
            {
                var ds = SqlHelper.ExecuteDataset(config.ConnectionString, CommandType.Text, config.XiashuSql);
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    var r = ds.Tables[0].Rows[i];
                    string a = r["namecn"] + ":" + SendXiaShu(r["namecn"], r["supname"], r["mail"]);
                    LogHelper.Debug(a);
                    Console.WriteLine(a);
                    Thread.Sleep(5000);
                }
            }
            Console.WriteLine("结束");
            Console.ReadLine();
        }

        static bool SendXiaShu(object name, object supName, object email)
        {
            try
            {
                var result = new MailHelper();
                AlternateView htmlBody = AlternateView.CreateAlternateViewFromString(xiashu(name, supName), null, "text/html");

                {
                    LinkedResource lrImage = new LinkedResource(PathHelper.MapPath("~/logo.png"), "image/png");
                    lrImage.ContentId = "logo";
                    htmlBody.LinkedResources.Add(lrImage);
                }
                result.MailMessage.AlternateViews.Add(htmlBody);
                return result.Step1_SmtpServer("smtp.188.com")
                        .Step2_Authorize("research2012", "es123456")
                        .Step3_MailContent("西安杨森管理人员反馈调研 (重要邮件，请不要删除)", null)
                        .Step4_To(email.ToString())
                        .Step6_FromAddress("research2012@188.com")
                        .Send();
            }
            catch
            {
                return false;
            }

        }

        static bool SendZiping(object name, object email)
        {
            try
            {
                var result = new MailHelper();
                AlternateView htmlBody = AlternateView.CreateAlternateViewFromString(ziping(name), null, "text/html");

                {
                    LinkedResource lrImage = new LinkedResource(PathHelper.MapPath("~/logo.png"), "image/png");
                    lrImage.ContentId = "logo";
                    htmlBody.LinkedResources.Add(lrImage);
                }
                result.MailMessage.AlternateViews.Add(htmlBody);
                return result.Step1_SmtpServer("smtp.188.com")
                        .Step2_Authorize("research2012", "es123456")
                        .Step3_MailContent("西安杨森管理人员反馈调研 (重要邮件，请不要删除)", null)
                        .Step4_To(email.ToString())
                        .Step6_FromAddress("research2012@188.com")
                        .Send();
            }
            catch
            {
                return false;
            }
        }

        #region 邮件内容
        private static string xiashu(object name, object supName)
        {
            return @"
<p>亲爱的" + name + @"：</p>

<p style='text-indent: 2em;'>正如总裁在最近的沟通信中所提到的， 我们正致力于建设高绩效的企业文化。Leading Coaching & the 5 Conversations 则作为持续推动团队高绩效的重要举措之一。</p>

<p style='text-indent: 2em;'>为了打造高质量持续辅导的文化，了解大家对于Leading Coaching & the 5 Conversations辅导的提高程度及实施的状况，人力资源部邀请您参加您的经理<b>" + supName + @"</b>的<b>管理人员反馈调研</b>，本调查问卷侧重于您的经理作为管理人员在绩效与发展中五次面谈的流程、技能及结果方面的优势和需改进的方面。</p>

<p style='text-indent: 2em;'>您的评价将被严格保密，人力资源部仅会对所有下属的反馈汇总结果。</p>

<p style='text-indent: 2em;'>请登录 <a href='http://221.209.110.83:90/'>http://221.209.110.83:90/</a> 参加本次调研（用户名及密码都是WWID号，即员工号）。</p>

<p style='text-indent: 2em;'> 再次感谢您参与本次调研！</p>

<p> <img src='cid:logo' /></p>
";
        }

        private static string ziping(object name)
        {
            return @"
<p>亲爱的" + name + @"：</p>

<p style='text-indent: 2em;'>正如总裁在最近的沟通信中所提到的， 我们正致力于建设高绩效的企业文化。Leading Coaching & the 5 Conversations 则作为持续推动团队高绩效的重要举措之一。</p>

<p style='text-indent: 2em;'>为了打造高质量持续辅导的文化，了解大家对于Leading Coaching & the 5 Conversations辅导的提高程度及实施的状况，人力资源部邀请您参加本次自评，旨在了解您本人作为管理人员对于自己在五个面谈的流程、技能及结果等方面的优势和需改进的方面。</p>

<p style='text-indent: 2em;'>请登录 <a href='http://221.209.110.83:90/'>http://221.209.110.83:90/</a> 参加本次调研（用户名及密码都是WWID号，即员工号）。</p>

<p style='text-indent: 2em;'> 再次感谢您参与本次调研！</p>

<p style='text-indent: 2em;'> <img src='cid:logo' /></p>
";
        }
        #endregion
    }

    public class EmailSql
    {
        public string XiashuSql { get; set; }

        public string ZipingSql { get; set; }

        public string ConnectionString { get; set; }
    }
}
